//
//  Decimal.swift
//  
//
//  Created by tangbl93 on 2021/1/12.
//

import Foundation

public extension Int {
    var yx_decimal: Decimal {
        return Decimal(integerLiteral: self)
    }
}
public extension Double {
    var yx_decimal: Decimal {
        return Decimal(floatLiteral: self)
    }
}

// Decimal is NSDecimalNumber's swift version
public extension Decimal {
        
    /// rounding number
    /// - Parameters:
    ///   - roundingMode: rounding up or down
    ///   - scale: float point keep
    /// - Returns: string literal
    func yx_rounding(roundingMode: NSDecimalNumber.RoundingMode = .down, scale: Int = 2) -> String {
        var number = withUnsafePointer(to: self) { $0.pointee }
        let result = UnsafeMutablePointer<Decimal>.allocate(capacity: 1)
        
        NSDecimalRound(result, &number, scale, roundingMode)
        
        return NSDecimalString(result, nil)
    }
}
